package com.ruoyi.web.controller.fantasy;

import java.util.Date;
import java.util.List;

import com.huanzhidadi.fantasy.bean.domain.FantasyCompletionCondition;
import com.huanzhidadi.fantasy.bean.domain.FantasyReward;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.fantasy.domain.*;
import com.ruoyi.fantasy.service.*;
import com.ruoyi.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.huanzhidadi.fantasy.bean.domain.FantasyTask;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 任务 信息操作处理
 * 
 * @author wanghui
 * @date 2019-07-05
 */
@Controller
@RequestMapping("/fantasy/fantasyTask")
public class FantasyTaskController extends BaseController
{
    private String prefix = "fantasy/fantasyTask";
	
	@Autowired
	private IFantasyTaskService fantasyTaskService;

	@Autowired
	private IFantasyNpcService fantasyNpcService;

	@Autowired
	private IFantasyItemtypeService fantasyItemtypeService;

	@Autowired
	private IFantasyRewardService fantasyRewardService;

	@Autowired
	private IFantasyTaskRewardService fantasyTaskRewardService;

	@Autowired
	private IFantasyCompletionConditionService fantasyCompletionConditionService;

	@Autowired
	private IFantasyTaskCompletionConditionService fantasyTaskCompletionConditionService;


	@RequiresPermissions("fantasy:fantasyTask:view")
	@GetMapping()
	public String fantasyTask()
	{
	    return prefix + "/fantasyTask";
	}
	
	/**
	 * 查询任务列表
	 */
	@RequiresPermissions("fantasy:fantasyTask:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(FantasyTask fantasyTask)
	{
		startPage();
        List<FantasyTask> list = fantasyTaskService.selectFantasyTaskList(fantasyTask);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出任务列表
	 */
	@RequiresPermissions("fantasy:fantasyTask:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FantasyTask fantasyTask)
    {
    	List<FantasyTask> list = fantasyTaskService.selectFantasyTaskList(fantasyTask);
        ExcelUtil<FantasyTask> util = new ExcelUtil<FantasyTask>(FantasyTask.class);
        return util.exportExcel(list, "fantasyTask");
    }
	
	/**
	 * 新增任务
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap) {
		List<FantasyNpc> fantasyNpcList = fantasyNpcService.selectFantasyNpcList(null);
		mmap.put("fantasyNpcList", fantasyNpcList);
		List<FantasyItemType> fantasyItemTypeList = fantasyItemtypeService.selectFantasyItemtypeList(null);
		mmap.put("fantasyItemTypeList", fantasyItemTypeList);
		List<FantasyTask> fantasyTaskList = fantasyTaskService.selectFantasyTaskList(null);
		mmap.put("fantasyTaskList", fantasyTaskList);
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存任务
	 */
	@RequiresPermissions("fantasy:fantasyTask:add")
	@Log(title = "任务", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(FantasyTask fantasyTask) {
		fantasyTask.setTaskId(String.valueOf(IdWorker.nextId()));
		fantasyTask.setCreateBy(ShiroUtils.getLoginName());
		fantasyTask.setCreateTime(new Date());
		return toAjax(fantasyTaskService.insertFantasyTask(fantasyTask));
	}

	/**
	 * 修改任务
	 */
	@GetMapping("/edit/{taskId}")
	public String edit(@PathVariable("taskId") String taskId, ModelMap mmap) {
		FantasyTask fantasyTask = fantasyTaskService.selectFantasyTaskById(taskId);
		mmap.put("fantasyTask", fantasyTask);
		List<FantasyNpc> fantasyNpcList = fantasyNpcService.selectFantasyNpcList(null);
		mmap.put("fantasyNpcList", fantasyNpcList);
		List<FantasyItemType> fantasyItemTypeList = fantasyItemtypeService.selectFantasyItemtypeList(null);
		mmap.put("fantasyItemTypeList", fantasyItemTypeList);
		List<FantasyTask> fantasyTaskList = fantasyTaskService.selectFantasyTaskList(null);
		mmap.put("fantasyTaskList", fantasyTaskList);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存任务
	 */
	@RequiresPermissions("fantasy:fantasyTask:edit")
	@Log(title = "任务", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(FantasyTask fantasyTask) {
		fantasyTask.setUpdateBy(ShiroUtils.getLoginName());
		fantasyTask.setUpdateTime(new Date());
		return toAjax(fantasyTaskService.updateFantasyTask(fantasyTask));
	}
	
	/**
	 * 删除任务
	 */
	@RequiresPermissions("fantasy:fantasyTask:remove")
	@Log(title = "任务", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(fantasyTaskService.deleteFantasyTaskByIds(ids));
	}


	/**
	 * 任务管理-分配奖励
	 */
	@GetMapping("/allocReward/{taskId}")
	public String allocReward(@PathVariable("taskId") String taskId, ModelMap mmap)
	{
		FantasyTask fantasyTask = fantasyTaskService.selectFantasyTaskById(taskId);
		mmap.put("fantasyTask", fantasyTask);
		return prefix + "/allocReward";
	}

	/**
	 * 查询已分配的奖励列表
	 */
	@RequiresPermissions("fantasy:fantasyTaskReward:list")
	@PostMapping("/allocReward/allocatedRewardList")
	@ResponseBody
	public TableDataInfo allocatedRewardList(FantasyTaskRewardBO fantasyTaskRewardBO)
	{
		startPage();
		List<FantasyTaskRewardBO> fantasyTaskRewardBOList = fantasyTaskRewardService.selectAllocatedList(fantasyTaskRewardBO);
		fantasyTaskRewardService.transforData(fantasyTaskRewardBOList);
		return getDataTable(fantasyTaskRewardBOList);
	}

	/**
	 * 取消分配
	 */
	@Log(title = "奖励分配管理", businessType = BusinessType.GRANT)
	@PostMapping("/allocReward/cancel")
	@ResponseBody
	public AjaxResult cancelAllocReward(FantasyTaskRewardBO fantasyTaskRewardBO)
	{
		return toAjax(fantasyTaskRewardService.deleteFantasyTaskReward(fantasyTaskRewardBO));
	}

	/**
	 * 批量取消分配
	 */
	@Log(title = "奖励分配管理", businessType = BusinessType.GRANT)
	@PostMapping("/allocReward/cancelAll")
	@ResponseBody
	public AjaxResult cancelAllocRewardAll(String taskId, String rewardIds)
	{
		return toAjax(fantasyTaskRewardService.deleteAllocRewards(taskId, rewardIds));
	}


	/**
	 * 选择奖励页面
	 */
	@GetMapping("/allocReward/selectReward/{taskId}")
	public String selectReward(@PathVariable("taskId") String taskId, ModelMap mmap)
	{
		FantasyTask fantasyTask = fantasyTaskService.selectFantasyTaskById(taskId);
		mmap.put("fantasyTask", fantasyTask);
		return prefix + "/selectReward";
	}

	/**
	 * 查询奖励列表
	 */
	@RequiresPermissions("fantasy:fantasyTaskReward:list")
	@PostMapping("/allocReward/rewardList")
	@ResponseBody
	public TableDataInfo rewardList(FantasyReward fantasyReward)
	{
		startPage();
		List<FantasyReward> list = fantasyRewardService.selectFantasyRewardList(fantasyReward);
		fantasyRewardService.transforData(list);
		return getDataTable(list);
	}

	/**
	 * 批量选择奖励
	 */
	@RequiresPermissions("fantasy:fantasyTaskReward:add")
	@Log(title = "奖励分配管理", businessType = BusinessType.GRANT)
	@PostMapping("/allocReward/selectAll")
	@ResponseBody
	public AjaxResult selectAllocRewardAll(String taskId, String rewardIds)
	{
		return toAjax(fantasyTaskRewardService.insertAllocRewards(taskId, rewardIds));
	}








	/**
	 * 任务管理-分配完成条件
	 */
	@GetMapping("/allocCompletionCondition/{taskId}")
	public String allocCompletionCondition(@PathVariable("taskId") String taskId, ModelMap mmap)
	{
		FantasyTask fantasyTask = fantasyTaskService.selectFantasyTaskById(taskId);
		mmap.put("fantasyTask", fantasyTask);
		return prefix + "/allocCompletionCondition";
	}

	/**
	 * 查询已分配的完成条件列表
	 */
	@RequiresPermissions("fantasy:fantasyCompletionCondition:list")
	@PostMapping("/allocCompletionCondition/allocatedCompletionConditionList")
	@ResponseBody
	public TableDataInfo allocatedCompletionConditionList(FantasyTaskCompletionConditionBO fantasyTaskCompletionConditionBO)
	{
		startPage();
		List<FantasyTaskCompletionConditionBO> fantasyTaskCompletionConditionBOList = fantasyTaskCompletionConditionService.selectAllocatedList(fantasyTaskCompletionConditionBO);
		fantasyTaskCompletionConditionService.transforData(fantasyTaskCompletionConditionBOList);
		return getDataTable(fantasyTaskCompletionConditionBOList);
	}

	/**
	 * 取消分配
	 */
	@Log(title = "完成条件分配管理", businessType = BusinessType.GRANT)
	@PostMapping("/allocCompletionCondition/cancel")
	@ResponseBody
	public AjaxResult cancelAllocCompletionCondition(FantasyTaskCompletionConditionBO fantasyTaskCompletionConditionBO)
	{
		return toAjax(fantasyTaskCompletionConditionService.deleteFantasyTaskCompletionCondition(fantasyTaskCompletionConditionBO));
	}

	/**
	 * 批量取消分配
	 */
	@Log(title = "完成条件分配管理", businessType = BusinessType.GRANT)
	@PostMapping("/allocCompletionCondition/cancelAll")
	@ResponseBody
	public AjaxResult cancelAllocCompletionConditionAll(String taskId, String completionIds)
	{
		return toAjax(fantasyTaskCompletionConditionService.deleteAllocCompletionConditions(taskId, completionIds));
	}


	/**
	 * 选择完成条件页面
	 */
	@GetMapping("/allocCompletionCondition/selectCompletionCondition/{taskId}")
	public String selectCompletionCondition(@PathVariable("taskId") String taskId, ModelMap mmap)
	{
		FantasyTask fantasyTask = fantasyTaskService.selectFantasyTaskById(taskId);
		mmap.put("fantasyTask", fantasyTask);
		return prefix + "/selectCompletionCondition";
	}

	/**
	 * 查询完成条件列表
	 */
	@RequiresPermissions("fantasy:fantasyCompletionCondition:list")
	@PostMapping("/allocCompletionCondition/completionConditionList")
	@ResponseBody
	public TableDataInfo completionConditionList(FantasyCompletionCondition fantasyCompletionCondition)
	{
		startPage();
		List<FantasyCompletionCondition> list = fantasyCompletionConditionService.selectFantasyCompletionConditionList(fantasyCompletionCondition);
		fantasyCompletionConditionService.transforData(list);
		return getDataTable(list);
	}

	/**
	 * 批量选择完成条件
	 */
	@RequiresPermissions("fantasy:fantasyCompletionCondition:add")
	@Log(title = "完成条件分配管理", businessType = BusinessType.GRANT)
	@PostMapping("/allocCompletionCondition/selectAll")
	@ResponseBody
	public AjaxResult selectAllocCompletionConditionAll(String taskId, String completionIds)
	{
		return toAjax(fantasyTaskCompletionConditionService.insertAllocCompletionConditions(taskId, completionIds));
	}

}
